// pages/consultationDetail/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nameText:'',
    ageText:'',
    genderData:[{
      type:'男',
      isChecked:true
    },{
        type: '女',
        isChecked: false
    }],
    symptomInfo:'',// 症状
    symptomInfoImages:[
      '../../images/add.png'

    ],//症状图片
    disabled:true,
    hasPhoneNum:false,
    uploadImgArr:[],
    isNeedBack:false,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      hasPhoneNum: app.hasPhoneNum
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },
  inputAction(e){
    console.log(e)
    if(e.currentTarget.id == "1"){
      this.setData({
        nameText:e.detail.value
      })
    }else{
      this.setData({
        ageText: e.detail.value
      })
    }
    this.checkIsDisabled()
  },
  // 性别切换
  clickRadio(e){
    console.log(e)
    var genderdata = this.data.genderData
    if(e.currentTarget.id == '1'){
      genderdata[0].isChecked = true
      genderdata[1].isChecked = false
    }else{
      genderdata[0].isChecked = false
      genderdata[1].isChecked = true
    }
    this.setData({
      genderData: genderdata
    })
  },
  // 症状描述
  textareaChange(e){
    console.log(e)
    this.setData({
      symptomInfo: e.detail.value
    })
    this.checkIsDisabled()
  },


  // 添加照片
  clickAddImage(e){
    console.log('11')
    var imgArr = this.data.symptomInfoImages
    if ((imgArr.length == 6 && imgArr[5] != '../../images/add.png')){
      wx.previewImage({
        urls: this.data.symptomInfoImages,
        current: this.data.symptomInfoImages[Number(e.currentTarget.id)]
      })
      return
    } else if (Number(e.currentTarget.id) != imgArr.length - 1){
      wx.previewImage({
        urls: this.data.symptomInfoImages.slice(0, imgArr.length-1),
        current: this.data.symptomInfoImages[Number(e.currentTarget.id)]
      })
      return
    }
    if (Number(e.currentTarget.id) != imgArr.length-1 || (imgArr.length == 6 && imgArr[5] != '../../images/add.png')){
      wx.previewImage({
        urls: this.data.symptomInfoImages,
        current: this.data.symptomInfoImages[Number(e.currentTarget.id)]
      })
      return
    }
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        console.log(res)
        imgArr = that.addImageAction(res.tempFilePaths[0])
        // imgArr = imgArr.splice(imgArr.length-2,1,res.tempFilePaths[0])
        console.log(imgArr)
        that.setData({
          symptomInfoImages: imgArr
        })
        that.checkIsDisabled()
      },
    })
  },
  addImageAction(img){
    var imgArr = []
    for (var i = 0; i < this.data.symptomInfoImages.length;i++){
      if(i==5){
        imgArr.push(img)
        this.uploadImage(img)
      }else if (i != this.data.symptomInfoImages.length-1){
        imgArr.push(this.data.symptomInfoImages[i])
      }else{
        imgArr.push(img)
        this.uploadImage(img)
        imgArr.push(this.data.symptomInfoImages[i])
      }
    }
    return imgArr
  },


   //提交咨询
  handleSubmit(){
    var that = this
    // wx.navigateTo({
    //   url: '/pages/submitSuccess/index',
    //   success(res){
    //     that.data.isNeedBack = true
    //   }
    // })
    
    // return
    if (that.data.disabled || !that.data.hasPhoneNum){
      return
    }
    wx.showLoading({
      title: '正在提交...',
    })
    app.post(app.Apis.SUBMIT_QUESTION,{
      userId:app.userId,
      mainStoreId:app.mainStoreId,
      realName: that.data.nameText,
      sex: that.data.genderData[0].isChecked ? 1 : 2,
      age: that.data.ageText,
      question: that.data.symptomInfo,
      imgUrls: JSON.stringify(that.data.uploadImgArr)
    },result=>{
      console.log(result)
      if (result.success){
      wx.navigateTo({
        url: '/pages/submitSuccess/index',
      })
      }
      wx.hideLoading()
    })

  },
  getPhoneNumber(e){
    console.log(e)
    var that = this
    app.getPhoneNum(e, result => {
      that.setData({
        hasPhoneNum: true
      })
      that.handleSubmit()
    })
  },
  

  checkIsDisabled(){
    if (this.data.nameText == '' || this.data.ageText == '' || this.data.symptomInfo.length < 10 || this.data.symptomInfoImages.length == 1){
      this.setData({
        disabled:true
      })
    }else{
      this.setData({
        disabled: false
      })
    }
    console.log(this.data.disabled)
  },
  uploadImage(tempFilePath){
    var that = this
    app.post(app.Apis.GET_UPLOAD_TOKEN, {
      imageType: 'CASE',
      fileType: 'jpg'
    }, result => {
      console.log('---------', result, '---------')
      wx.uploadFile({
        url: 'https://up.qbox.me/',
        filePath: tempFilePath,
        name: 'file',
        formData: {
          'token': result.uptoken.uptoken,
          'key': result.uptoken.imgName
        },
        success: function (res) {
          var data = res.data
          console.log(res)
          console.log(result)
          that.data.uploadImgArr.push(result.uptoken.imgUrl)
        }
      })
    })
  }
})